package com.victor.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.entity.Banner;
import com.victor.service.BannerService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * banner管理
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private HttpServletRequest request;

	// 获取所有banner
	@RequestMapping("/getAll")
	public Msg getBannerList() {
		List<Banner> banners = bannerService.getAllBanner();
		return Msg.success().add("banners", banners);
	}

	//修改banner
	@RequestMapping("/update")
	public Msg updateBanner(Banner banner, MultipartFile file) throws Exception {		
		if (file != null) {
			String filepath = FileUpload.uploadFile(file, request);
			banner.setBanner_path(filepath);
		}
		boolean updResult = bannerService.updateBanner(banner);
		return updResult ? Msg.success() : Msg.fail();
	}
}
